<?php

namespace App\Http\Controllers;

use App\Http\Requests\StorePostRequest;
use App\Models\Post;
use Illuminate\Http\Request;

class CommentController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StorePostRequest $request)
    {
        $data = $request->all();
        $data['user_id'] = Auth::id();
        dd($data);
        $res = Comment::create($data);

        if($res){
            $post = Post::find($request->post_id);
            dd($post);
            $post->increment('comments');
            $post->last_reply_name = Auth::user()->name;
            $post->save();

            return redirect()->route('post.show',[$request->post_id]);
        }
    }



    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
